package com.penghaisoft.microwms.base.web.data.controller;

import com.penghaisoft.microwms.base.web.data.service.IMdWarehouseService;
import com.penghaisoft.microwms.common.controller.BaseController;
import com.penghaisoft.microwms.common.data.model.MdWarehouse;
import com.penghaisoft.microwms.common.dto.Resp;
import com.penghaisoft.microwms.common.dto.TransResult;
import com.penghaisoft.microwms.common.page.Pager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 仓库管理
 * @Param:
 * @return:
 * @Author: zhangx
 * @Date: 2018/4/27 15:04
 */
@RestController
@RequestMapping(value = "/data/warehouse")
@Slf4j
public class MdWarehouseController extends BaseController {

	@Autowired
	private IMdWarehouseService mdWarehouseService;

	/** 
	 * @Title create
	 * @Description 新增记录
	 * @author gaofeng
	 * @date 2017年5月24日 下午1:58:43
	 * @param mdWarehouse
	 * @return TransResult
	 */
	@PostMapping("c")
	public TransResult create(MdWarehouse mdWarehouse) {
		mdWarehouse.preInsert();
		mdWarehouse.setFactoryCode(getCurrentFactoryCode());
		mdWarehouse.setFactoryName(getCurrentUserFactoryDto().getFactoryName());
		mdWarehouse.setDataSource("");
		mdWarehouse.setSchame("");
        mdWarehouse.setCreateBy(getLoginName());
        mdWarehouse.setLastModifiedBy(getLoginName());
		Resp resp = new Resp();
		MdWarehouse tmp = mdWarehouseService.findByCode(mdWarehouse);
		if(tmp == null){
		    resp = mdWarehouseService.create(mdWarehouse);
		} else {
			resp.setMsg("base.data.warehouse.conflict");
		}
		return fromResp(resp);
	}

	/** 
	 * @Title delete
	 * @Description 删除记录（逻辑删除active_flag=0)
	 * @author gaofeng
	 * @date 2017年5月24日 下午1:59:00
	 * @param mdWarehouse
	 * @return TransResult
	 */
	@PostMapping("d")
	public TransResult delete(MdWarehouse mdWarehouse) {
		mdWarehouse.preUpdate();
		Resp resp = mdWarehouseService.delete(mdWarehouse);
		return fromResp(resp);
	}

	/** 
	 * @Title list
	 * @Description 查询列表
	 * @author gaofeng
	 * @date 2017年5月24日 下午1:59:15
	 * @param page
	 * @param rows
	 * @param mdWarehouse
	 * @return TransResult
	 */
	@PostMapping("list")
	public TransResult list(@RequestParam(name = "page", defaultValue = "1") int page,
                            @RequestParam(name = "rows", defaultValue = "10") int rows, MdWarehouse mdWarehouse) {
		mdWarehouse.setFactoryCode(getCurrentFactoryCode());
		Pager<MdWarehouse> result = mdWarehouseService.findListByCondition(page, rows, mdWarehouse);
		return success(result);
	}

	/** 
	 * @Title queryById
	 * @Description 查询单条
	 * @author gaofeng
	 * @date 2017年5月24日 下午1:59:30
	 * @param id
	 * @return TransResult
	 */
	@GetMapping("r/{id}")
	public TransResult queryById(@PathVariable String id) {
		MdWarehouse mdWarehouse = mdWarehouseService.findById(id);
		return success(mdWarehouse);
	}

	/** 
	 * @Title update
	 * @Description 修改记录
	 * @author gaofeng
	 * @date 2017年5月24日 下午1:59:48
	 * @param mdWarehouse
	 * @return TransResult
	 */
	@PostMapping("u")
	public TransResult update(MdWarehouse mdWarehouse) {
		mdWarehouse.preUpdate();
		Resp resp = mdWarehouseService.update(mdWarehouse);
		return fromResp(resp);
	}
	
	/** 
	 * @Title queryWarehousesUnderFactory
	 * @Description 查询某个工厂下所有的仓库
	 * @author zhangxu
	 * @date 2017年5月24日 下午2:00:09
	 * @param factoryCode
	 * @return TransResult
	 */
	@PostMapping("r/{factoryCode}/warehouse")
	public TransResult queryWarehousesUnderFactory(@PathVariable String factoryCode) {
		MdWarehouse condition = new MdWarehouse();

		if ("all".equals(factoryCode)) {
			factoryCode = null;
		}
		//condition.setFactoryCode(factoryCode);
		condition.setFactoryCode(getCurrentFactoryCode());
		List<MdWarehouse> warehouses = mdWarehouseService.queryWarehousesUnderFactory(condition);
		return success(warehouses);
	}
	
	/** 
	 * @Title queryWarehousesUnderFactory1
	 * @Description 查询当前工厂下的成品仓库
	 * @author luoteng
	 * @date 2017年11月2日:下午5:13:52
	 * @return TransResult
	 */
	@PostMapping("r/factory/product/warehouse")
	public TransResult queryWarehousesUnderFactory1() {
		MdWarehouse condition = new MdWarehouse();

		condition.setFactoryCode(getCurrentFactoryCode());
//		1WMS原材料, 2wms成品
//		condition.setWarehouseType("2");
		List<MdWarehouse> warehouses = mdWarehouseService.queryWarehousesUnderFactory(condition);
		return success(warehouses);
	}
	
	/** 
	 * @Title queryWarehousesUnderFactory2
	 * @Description 查询所有外库工厂下的仓库
	 * @author luoteng
	 * @date 2017年11月2日:下午5:21:41
	 * @return TransResult
	 */
	@PostMapping("r/wk/warehouse")
	public TransResult queryWarehousesUnderFactory2() {
		MdWarehouse condition = new MdWarehouse();

		condition.setFactoryCodeWk("wk");
		List<MdWarehouse> warehouses = mdWarehouseService.queryWarehousesUnderFactory(condition);
		return success(warehouses);
	}

	
	@PostMapping("r/{factoryCode}/warehouse1")
	public TransResult queryWarehousesUnderFactory1(@PathVariable String factoryCode) {
		MdWarehouse condition = new MdWarehouse();

		if ("all".equals(factoryCode)) {
			factoryCode = null;
		}
		//condition.setFactoryCode(factoryCode);
		condition.setFactoryCode(getCurrentFactoryCode());
		condition.setWarehouseType("2");
		List<MdWarehouse> warehouses = mdWarehouseService.queryWarehousesUnderFactory(condition);
		return success(warehouses);
	}
}
